package com.dau.api.controller;

import cn.hutool.core.lang.UUID;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import com.dau.common.util.MinioUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 *
 * @author tzj
 * @date 2024/11/26
 */
@RestController
@Slf4j
@RequestMapping("/api")
public class ApiController {
    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    private MinioUtil minioUtil;

    @GetMapping("/rabbit/send/direct")
    public void direct() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：NewDirectRouting 发送到交换机NewDirectExchange
        rabbitTemplate.convertAndSend("NewDirectExchange", "NewDirectRouting", map);
    }

    @GetMapping("/rabbit/send/topic")
    public void topic() {
        String messageData = "test message,  topic.user come in!";
        rabbitTemplate.convertAndSend("topicExchange", "topic.user", messageData);
    }

    @GetMapping("/rabbit/send/fanout")
    public void fanout() {
        String messageData = "test message, new user come in!";
        rabbitTemplate.convertAndSend("fanoutExchange", null, messageData);
    }

    @GetMapping("/rabbit/send/error")
    public void error() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, new user come in!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("error", "test", map);
    }

    @GetMapping("/rabbit/send/lonely")
    public void lonely() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, new user come in!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "NewDirectRouting", map);
    }

    @GetMapping("/rabbit/send/dead")
    public void dead() {
        String messageData = "test message, dead come in!";
        rabbitTemplate.convertAndSend("delay.exchange.name", "delay.key.name", messageData, message -> {
            // 添加延迟消息属性
            message.getMessageProperties().setExpiration("5000");
            return message;
        });
    }
    /**
     * 上传文件
     */
    @PostMapping(value = "/upload")
    public String uploadReport(MultipartFile[] files) {
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            minioUtil.upload(file, fileName);
        }
        return "上传成功";
    }

    /**
     * 预览文件
     */
    @GetMapping("/preview")
    public String preview(String fileName) {
        return minioUtil.getFileUrl(fileName);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    public void download(String fileName, HttpServletResponse response) {
        minioUtil.download(response, fileName);
    }

    /**
     * 删除文件
     */
    @GetMapping("/delete")
    public String delete(String fileName) {
        minioUtil.delete(fileName);
        return "删除成功";
    }
}
